#ifndef JPROCESS_P_H
#define JPROCESS_P_H

// W A R N I N G
// -------------
// This file is not JXQLIB-APIs

#include "jprocess.h"
#include "jabstractio_p.h"
#include <JXQCore/JThread>
#include <JXQCore/JMutex>
#include <JXQCore/JDebug>
#include <JXQIO/JCProcessWatcher>
#include <JXQIO/JIONotifier>
#include <JXQIO/JBuffer>
#include <condition_variable>
#include <atomic>

namespace JXQ
{

typedef JProcess::ProcessMain  ProcessMain;
typedef JProcess::Channel      Channel;
typedef JProcess::Result       Result;
typedef JProcess::State        State;

class J_DECL_HIDDEN JProcessPrivate :
	public JAbstractIOPrivate, public JMultiIONotifier
{
	J_DECLARE_PUBLIC(JProcess)

public:
	explicit JProcessPrivate(JProcess *q_ptr);

public:
	std::atomic<JProcessWatcher*> m_watcher {nullptr};
	std::atomic_int m_result {0};

public:
	std::mutex pipeWriteLock;
	int m_pipeFd[3][2] {
		{-1,-1}, {-1,-1}, {-1,-1} };

	std::atomic<Channel> m_channel {JProcess::StdOut};
	JQueueBuffer m_buffer[3];

public:
	int pipe();
	void createWatcher(JHandle pid);
	JString read(int channel);

protected:
	void event(int fd, JIONotifier::Type event) override;
};

} //namespace JXQ


#endif //JPROCESS_P_H

